/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.MenuForm;
import com.uca.sisbec.helpers.manto.PantallaHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Juank
 */
public class MenuAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward guardarOrdenMenus(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MenuForm mf = (MenuForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        helper.guardarOrdenMenus(mf.getMenus());
        mf.setMenus(new String[0]);
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward showOptions(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MenuForm mf = (MenuForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        request.setAttribute("menuOptionsList", helper.showOptionsMenu(mf.getMenu()));
        mf.setMenus(new String[0]);
        mf.setOpciones(new String[0]);
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward guardarOrdenOpciones(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        MenuForm mf = (MenuForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        helper.guardarOrdenOpciones(mf.getOpciones());
        mf.setMenus(new String[0]);
        mf.setOpciones(new String[0]);
        setOptionsList(request);
        return showOptions(mapping, form, request, response);
    }

    private void setOptionsList(HttpServletRequest request) {
        PantallaHelper helper = new PantallaHelper(getSession(request));
        request.setAttribute("menuList", helper.getParentList());
    }
    
}
